package utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class AnJieSMS {
	/**
     * 生成加密sign
     * 安杰短信平台
     * @param timestamp 时间戳
     * @param noncestr  随机字符串
     * @return
     * @throws Exception
     */
    public static String getBizSign(String timestamp, String noncestr) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("token,", "Ubox_20140512#sendSms");
        map.put("timestamp,", timestamp);
        map.put("noncestr,", noncestr);
        HashMap<String, String> bizParameters = new HashMap<String, String>();
        List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1,
                               Map.Entry<String, String> o2) {
                return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });
        for (int i = 0; i < infoIds.size(); i++) {
            Map.Entry<String, String> item = infoIds.get(i);
            if (item.getKey() != "") {
                bizParameters.put(item.getKey().toLowerCase(), item.getValue());
            }
        }
        String bizString = formatBizQueryParaMap(bizParameters);
        String sign = "";
        try {
        // 将字符串进行sha1加密
        byte[] digest = MessageDigest.getInstance("SHA-1").digest(bizString.getBytes());
        // 将byte数组转换为16进制表示的字符串
            sign = new BigInteger(1, digest).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sign;
    }
    
/**
     * 格式化参数
     *
     * @param paraMap
     * @return
     * @throws Exception
     */
    public static String formatBizQueryParaMap(HashMap<String, String> paraMap) {
        String buff = "";
        List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(paraMap.entrySet());
        Collections.sort(infoIds,
                new Comparator<Map.Entry<String, String>>() {
                    public int compare(Map.Entry<String, String> o1,
                                       Map.Entry<String, String> o2) {
                        return (o1.getKey()).toString().compareTo(
                                o2.getKey());
                    }
                });

        for (int i = 0; i < infoIds.size(); i++) {
            Map.Entry<String, String> item = infoIds.get(i);
            if (item.getKey() != "") {

                String key = item.getKey();
                String val = item.getValue();
                buff += key.toLowerCase() + "=" + val + "&";
            }
        }

        if (buff.isEmpty() == false) {
            buff = buff.substring(0, buff.length() - 1);
        }
        return buff;
    }
    public static long send(String msg,String phone) throws Exception{
    	
    	String requestUrl="http://42.121.192.78:8086/ourangel-box-rpc/ubox/requestSms.html";
    	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    	Long TIMESTAMP=System.currentTimeMillis();
    	String noncestr="whutLoveYouBox";
    	nameValuePairs.add(new BasicNameValuePair("sign",getBizSign(TIMESTAMP.toString(),noncestr)));
    	nameValuePairs.add(new BasicNameValuePair("timestamp",TIMESTAMP.toString()));
    	nameValuePairs.add(new BasicNameValuePair("noncestr",noncestr));
    	nameValuePairs.add(new BasicNameValuePair("phone",phone));
    	nameValuePairs.add(new BasicNameValuePair("content",msg));
    	String responseString=Http.postByPair(requestUrl,nameValuePairs);
    	System.out.println(responseString);
    	JSONObject responseJSON=JSONObject.fromObject(responseString);
    	System.out.println("status: "+responseJSON.get("status"));
    	System.out.println("msgId: "+responseJSON.get("msgId"));
    	System.out.println("description: "+responseJSON.get("description"));
    	return Long.parseLong(responseJSON.get("msgId").toString());
    }
    
    
    public static void main(String[] args) {
//    	System.out.println(getBizSign("189026618","adss12313d"));
    	try {
			send("您的顺丰快递包裹fg12343d56456234已存入交通厅网点邮宝智能快递箱一号机1号箱,取件码:123456.服务号码:18144000235.","13559455537");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
